<template>
    <div>
        <el-card>
            <el-row :gutter="30">
                <el-col :span="10">
                    <el-input placeholder="请输入角色名称" v-model="query.roleName" clearable @clear="getRoleList">
                        <el-button slot="append" icon="el-icon-search" @click="getRoleList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加角色</el-button>
                </el-col>
            </el-row>
            <br />
            <el-table :data="roleList" border stripe>
                <el-table-column type="id" v-if="false">
                </el-table-column>
                <el-table-column prop="roleName" label="角色名称" align="center">
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center">
                </el-table-column>
                <el-table-column prop="updateTime" label="更新时间" align="center">
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tooltip class="itemStatus" effect="dark" :content="scope.row.status == 0 ? '点击启用' : '点击停用' "
                            placement="right">
                            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                                @change="updateStatus(scope.row)"></el-switch>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog title="添加员工" :visible.sync="addDialogVisible" @open="resetAddRoleForm" width="30%">
            <el-form :model="addRoleForm" ref="roleFormRef" label-width="20%">
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="addRoleForm.roleName"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addRole">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        created() {
            this.getRoleList();
        },
        data() {
            return {
                query: {
                    roleName: null,
                },
                roleList: [],
                addDialogVisible: false,
                editFlag: false,
                addRoleForm: {
                    id: null,
                    roleName: null,
                    rules: '1,7,11',
                },
            }
        },
        methods: {
            async getRoleList() {
                if (this.query.roleName === '') {
                    this.query.roleName = null;
                }
                const { data: res } = await this.$http.get("/sys/role/list", { params: this.query });
                this.roleList = res.data;
            },
            async addRole() {
                const { data: res } = await this.$http.post("/sys/role/add", this.addRoleForm);
                this.addDialogVisible = false;
                this.resetAddRoleForm();
                if (res.data) {
                    this.$message.success((this.addRoleForm.id == null ? "添加" : "编辑") + "角色成功!");
                    this.getRoleList();
                }
            },
            async updateStatus(row) {
                const { data: res } = await this.$http.post("/sys/role/update", row);
                if (!res.data) {
                    this.getRoleList();
                }
            },
            handleEdit(row) {

            },
            handleDelete(id) {
                this.$elementMessageBox.confirm('删除后将无法恢复, 确认删除吗? ! ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(async () => {
                        const { data: res } = await this.$http.delete("/sys/role/delete/" + id);
                        if (res.data) {
                            this.$message.success("删除成功!");
                            this.getRoleList();
                            return;
                        }
                        this.$message.error("删除失败!");
                    }).catch(() => {
                        this.$message.info("取消删除!");
                    });
            },
            resetAddRoleForm() {
                try {
                    this.$refs.roleFormRef.resetFields();
                } catch (error) {

                }
            },
        },
    }
</script>